<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      div {
        width: 100px;
        height: 100px;
      }
      .box1 {
        background: red;
      }
      .box2 {
        background: skyblue;
      }
      .box3 {
        background: pink;
      }
    </style>
  </head>
  <body>
    <button>按钮一</button>
    <button>按钮二</button>
    <button>按钮三</button>
    <script>
      var divEle = document.createElement("div");
      document.body.appendChild(divEle);
      var btns = document.querySelectorAll("button");
      //for each循环
      Array.from(btns).forEach(function (item, i, arr) {
        item.onclick = function () {
          divEle.className = `box${i + 1}`;
        };
      });
    </script>
  </body>
</html>
